package com.yict.csms.system.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yict.common.entity.PageEntity;
import com.yict.common.service.impl.CommonService;
import com.yict.csms.resourceplan.entity.RtgcFlWhartNeed;
import com.yict.csms.resourceplan.entity.RtgcFlWhartNeedHour;
import com.yict.csms.system.dao.impl.WharfDaoImpl;
import com.yict.csms.system.entity.Wharf;
import com.yict.csms.system.service.IWharfService;

/**
 * 
 * 
 * @author Patrick.Deng
 * 
 */
@Service
public class WharfServiceImpl extends CommonService<Wharf, Long> implements
		IWharfService {
	@Autowired
	private WharfDaoImpl wharfDaoImpl;

	public List<Wharf> search(Map<String, Object> queryMap,
			PageEntity page) {
		StringBuilder queryString = new StringBuilder();
		pageEntity = new PageEntity();
		//queryString.append(" from Wharf as c order by createDate asc");
		queryString.append(" from Wharf as c where 1=1");
		if (queryMap != null && queryMap.size() > 0) {
			for (Map.Entry<String, Object> entry : queryMap.entrySet()) {
				queryString.append(" and c.").append(entry.getKey()).append("=:").append(entry.getKey());
			}
		}
		queryString.append(" order by c.wharfsegment");
		List<Wharf> list = wharfDaoImpl.search(queryString.toString(),
				queryMap, page);
		pageEntity.setTotalRecord(wharfDaoImpl.getPageEntity()
				.getTotalRecord());
		return list;
	}

	public List<Wharf> searchAndNeed(Map<String, Object> queryMap,
			PageEntity page) {
		StringBuilder queryString = new StringBuilder();
		pageEntity = new PageEntity();
		queryString.append(" from Wharf as c left join RtgcFlWhartNeed as r");
		List<Wharf> list = wharfDaoImpl.search(queryString.toString(),
				queryMap, page);
		pageEntity.setTotalRecord(wharfDaoImpl.getPageEntity()
				.getTotalRecord());
		return list;
	}
	
	public RtgcFlWhartNeed findBySegment(String Segment,String timeId) {
		return wharfDaoImpl.findBySegment(Segment,timeId);
	}

	public RtgcFlWhartNeedHour findNeedHourBySegment(String Segment,String timeId) {
		return wharfDaoImpl.findByNeedHourSegment(Segment,timeId);
	}
	
	public Wharf findById(Long id) {
		return wharfDaoImpl.findById(id);
	}

	private String changeOrder(PageEntity entity) {
		String str = " order by c.";
		if (entity != null && entity.getField() != null
				&& !entity.getField().equals("")) {
			if (entity.getField().equals("companySName"))
				str += "companySName ";
			else if (entity.getField().equals("companyName"))
				str += "companyName ";
			else if (entity.getField().equals("license"))
				str += "license ";
			else if (entity.getField().equals("endBussDate"))
				str += "endBussDate ";
			else if (entity.getField().equals("legalPerson"))
				str += "legalPerson ";
			else if (entity.getField().equals("address"))
				str += "address ";
			else if (entity.getField().equals("website"))
				str += "website ";
			else if (entity.getField().equals("tel"))
				str += "tel ";
			else if (entity.getField().equals("fax"))
				str += "fax ";
			else if (entity.getField().equals("payBank"))
				str += "payBank ";
			else if (entity.getField().equals("payBankNum"))
				str += "payBankNum ";
			else
				str += "createDate desc";
			str += entity.getOrder();
		} else {
			str += "createDate desc";
		}
		return str;
	}
}
